package com.java.tutorials.spring.BeanFactoryPostProcessor;

import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
/**
 * Created by xuzhike on 2016/2/1.
 */
public class BeanFactoryPostProcessorTest1{
    @Test
    public void test(){
//        XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Spring-database.xml"));//这个是被废弃的
//        DefaultListableBeanFactory  factory = new DefaultListableBeanFactory((BeanFactory) new ClassPathResource("Spring-database.xml"));//这个有问题
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(new ClassPathResource("Spring-database.xml"));
        AllBeansLister lister = new AllBeansLister();
        lister.postProcessBeanFactory(factory);

        factory.getBean("dataSource");
    }
}

class AllBeansLister implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
            throws BeansException {

        System.out.println("The factory contains the followig beans:");
        String[] beanNames = factory.getBeanDefinitionNames();
        for (int i = 0; i < beanNames.length; ++i)
            System.out.println(beanNames[i]);
    }
}

class AllBeansLister2 implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {

        System.out.println("The factory contains the followig beans----------:");
        String[] beanNames = factory.getBeanDefinitionNames();
        for (int i = 0; i < beanNames.length; ++i)
            System.out.println(beanNames[i]);
    }
}
